package com.example.mytips.jsonToFlutterEntity;

import com.example.mytips.utils.StrUtil;
import com.intellij.openapi.ui.Messages;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MyEntityLog {

    public static String log(String className, Map<String, Object> map) {
        DartEntity dartEntity = new DartEntity();
        dartEntity.className = className;
        parseMap(dartEntity, map);

        List list = new ArrayList<String>();
        list.add("import 'package:mybase/base/tool/entitylog/MyEntityLog.dart';\n");
        printDartEntity(list, dartEntity);

        StringBuilder builder = new StringBuilder();
        for (Object s : list) {
            builder.append(s.toString()).append("\n");
        }
        return builder.toString();
    }

    public static void printDartEntity(List list, DartEntity dartEntity) {
        list.addAll(dartEntity.toText());
        list.add("");

        if (dartEntity.dartEntityList != null && !dartEntity.dartEntityList.isEmpty()) {
            for (DartEntity entity : dartEntity.dartEntityList) {
                printDartEntity(list, entity);
            }
        }
    }


    // 解析map
    private static void parseMap(DartEntity dartEntity, Map<?, ?> map) {

        // 遍历 Map，并根据值的类型执行不同的方法
        for (Map.Entry<?, ?> entry : map.entrySet()) {
            Object value = entry.getValue();
            String key = entry.getKey().toString();
            PropertyEntity property = null;
            if (value instanceof Integer) {
                property = handleIntegerValue(key, (Integer) value);
            } else if (value instanceof String) {
                property = handleStringValue(key, (String) value);
            } else if (value instanceof Boolean) {
                property = handleBooleanValue(key, (Boolean) value);
            } else if (value instanceof Double) {
                property = handleDoubleValue(key, (Double) value);
            } else if (value instanceof Map<?, ?>) {
                handleMapValue(dartEntity, key, (Map<?, ?>) value);
            } else if (value instanceof List<?>) {
                handleListValue(dartEntity, key, (List<?>) value);
            } else {
                // 处理其他类型的值
                handleOtherValue(value);
            }

            if (property != null) {
                dartEntity.propertyList.add(property);
            }
        }
    }

    // 处理 Integer 类型的值
    private static PropertyEntity handleIntegerValue(String key, Integer value) {
        PropertyEntity property = new PropertyEntity();
        property.property = "String? " + key + ";";
        property.construct = "this." + key + ",";
        property.fromJson = key + " = MyEntityLog.handleString(json, '" + key + "');";
        property.toJson = "data['" + key + "'] = this." + key + ";";
        return property;
    }

    // 处理 String 类型的值
    private static PropertyEntity handleStringValue(String key, String value) {
        PropertyEntity property = new PropertyEntity();
        property.property = "String? " + key + ";";
        property.construct = "this." + key + ",";
        property.fromJson = key + " = MyEntityLog.handleString(json, '" + key + "');";
        property.toJson = "data['" + key + "'] = this." + key + ";";
        return property;
    }

    // 处理 Boolean 类型的值
    private static PropertyEntity handleBooleanValue(String key, Boolean value) {
        PropertyEntity property = new PropertyEntity();
        property.property = "bool? " + key + ";";
        property.construct = "this." + key + ",";
        property.fromJson = key + " = MyEntityLog.handleBool(json, '" + key + "');";
        property.toJson = "data['" + key + "'] = this." + key + ";";
        return property;
    }

    // 处理 Double 类型的值
    private static PropertyEntity handleDoubleValue(String key, Double value) {
        PropertyEntity property = new PropertyEntity();
        property.property = "String? " + key + ";";
        property.construct = "this." + key + ",";
        property.fromJson = key + " = MyEntityLog.handleString(json, '" + key + "');";
        property.toJson = "data['" + key + "'] = this." + key + ";";
        return property;
    }

    // 处理 Map 类型的值
    private static void handleMapValue(DartEntity dartEntity, String key, Map<?, ?> value) {
        String className = StrUtil.toBigFirstWorld(key);

        PropertyEntity property = new PropertyEntity();
        property.property = className + "? " + key + ";";
        property.construct = "this." + key + ",";

        StringBuilder fromJson = new StringBuilder();
        fromJson.append("    if (json.containsKey('" + key + "')) {");
        fromJson.append("      var tmp = json['" + key + "'];");
        fromJson.append("      if (tmp is Map) {");
        fromJson.append("        " + key + " = json['" + key + "'] != null");
        fromJson.append("            ? new " + className + ".fromJson(json['" + key + "'])");
        fromJson.append("            : null;");
        fromJson.append("      } else {");
        fromJson.append("        " + key + " = null;");
        fromJson.append("      }");
        fromJson.append("    } else {");
        fromJson.append("      " + key + " = null;");
        fromJson.append("    }");

        property.fromJson = fromJson.toString();

        StringBuilder toJson = new StringBuilder();
        toJson.append("    if (this." + key + " != null) {");
        toJson.append("      data['" + key + "'] = this." + key + "!.toJson();");
        toJson.append("    }");

        property.toJson = toJson.toString();
        dartEntity.propertyList.add(property);

        DartEntity childDartEntity = new DartEntity();
        childDartEntity.className = className;
        parseMap(childDartEntity, value);
        dartEntity.dartEntityList.add(childDartEntity);
    }

    // 处理 List 类型的值
    private static void handleListValue(DartEntity dartEntity, String key, List<?> listValue) {
        String className = StrUtil.toBigFirstWorld(key);

        Object value = listValue.get(0);
        if (value instanceof Integer || value instanceof String || value instanceof Double || value instanceof Boolean) {
            String dataType = value instanceof Boolean ? "bool" : "String";

            PropertyEntity property = new PropertyEntity();
            property.property = "List<" + dataType + ">? " + key + ";";
            property.construct = "this." + key + ",";

            StringBuilder fromJson = new StringBuilder();
            fromJson.append("    if (json.containsKey('" + key + "')) {");
            fromJson.append("      var tmp = json['" + key + "'];");
            fromJson.append("      if (tmp is List) {");
            fromJson.append("        " + key + " = json['" + key + "'].cast<" + dataType + ">();");
            fromJson.append("      } else {");
            fromJson.append("        " + key + " = [];");
            fromJson.append("      }");
            fromJson.append("    } else {");
            fromJson.append("      " + key + " = [];");
            fromJson.append("    }");
            property.fromJson = fromJson.toString();

            property.toJson = "    data['" + key + "'] = this." + key + ";";
            dartEntity.propertyList.add(property);

        } else if (value instanceof Map<?, ?>) {

            PropertyEntity property = new PropertyEntity();
            property.property = "List<" + className + ">? " + key + ";";
            property.construct = "this." + key + ",";

            StringBuilder fromJson = new StringBuilder();
            fromJson.append("    if (json.containsKey('" + key + "')) {");
            fromJson.append("      if (json['" + key + "'] != null && json['" + key + "'] is List) {");
            fromJson.append("        " + key + " = <" + className + ">[];");
            fromJson.append("        json['" + key + "'].forEach((v) {");
            fromJson.append("          " + key + "!.add(new " + className + ".fromJson(v));");
            fromJson.append("        });");
            fromJson.append("      } else {");
            fromJson.append("        " + key + " = [];");
            fromJson.append("      }");
            fromJson.append("    } else {");
            fromJson.append("      " + key + " = [];");
            fromJson.append("    }");
            property.fromJson = fromJson.toString();

            StringBuilder toJson = new StringBuilder();
            toJson.append("    if (this." + key + " != null) {");
            toJson.append("      data['" + key + "'] = this." + key + "!.map((v) => v.toJson()).toList();");
            toJson.append("    }");
            property.toJson = toJson.toString();
            dartEntity.propertyList.add(property);


            DartEntity childDart = new DartEntity();
            childDart.className = className;
            parseMap(childDart, (Map<?, ?>) value);

            dartEntity.dartEntityList.add(childDart);


        } else if (value instanceof List<?>) {
            // TODO : 不处理

        } else {
            // TODO : 不处理
        }
    }

    // 处理其他类型的值
    private static void handleOtherValue(Object value) {
        System.out.println("Other value: " + value);
        // 在这里执行你的方法
    }
}


// dart类
class DartEntity {
    // 类名称
    String className;

    // 属性列表
    List<PropertyEntity> propertyList = new ArrayList<>();

    // 子类列表
    List<DartEntity> dartEntityList = new ArrayList<>();

    public List toText() {
        List<String> list = new ArrayList<>();

        list.add("class " + className + " {");
        toProperty(list);
        toConstruct(list);
        toFromJson(list);
        toJson(list);
        list.add("}");

        return list;

    }

    // 属性
    public void toProperty(List<String> list) {
        for (PropertyEntity propertyEntity : propertyList) {
            list.add("  " + propertyEntity.property);
        }
        list.add("");
    }

    // 构造函数
    public void toConstruct(List<String> list) {
        list.add("  " + className + "({");
        for (PropertyEntity propertyEntity : propertyList) {
            list.add("    " + propertyEntity.construct);
        }
        list.add("  });\n");
    }

    // fromJson
    public void toFromJson(List<String> list) {
        list.add("  " + className + ".fromJson(Map<String, dynamic> json) {");
        for (PropertyEntity propertyEntity : propertyList) {
            list.add("    " + propertyEntity.fromJson);
        }
        list.add("  }\n");
    }

    // toJson
    public void toJson(List<String> list) {
        list.add("  Map<String, dynamic> toJson() {");
        list.add("    final Map<String, dynamic> data = new Map<String, dynamic>();");
        for (PropertyEntity propertyEntity : propertyList) {
            list.add("    " + propertyEntity.toJson);
        }
        list.add("    return data;");
        list.add("  }");
    }
}

// 属性
class PropertyEntity {
    String property;
    String construct;
    String fromJson;
    String toJson;
}